home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_pas / lzw4p14.zip / RW_IO.PAS < prev    next >
Pascal/Delphi Source File  |  1994-05-30  |  4KB  |  151 lines

  1. (*   RW_IO.PAS
  2. **
  3. **   Reader/Writer Buffered I/O
  4. **
  5. **   Reader() and Writer() are called directly by the LZW4PLIB.ASM code.
  6. **   They should never be called by your application code.
  7. **
  8. **   The other functions are never called by the LZW4PLIB.ASM code, but are
  9. *    called only by your application routines.
  10. **
  11. **   Note that only a Reader() and Writer() function is required by the
  12. **   LZW4PLIB.ASM code. This means that you have complete control over data
  13. **   coming into and out of the compression/expansion code. Instead of
  14. **   reading or writing to disk, you can just as easily read/write to a
  15. **   buffer, serial port, etc. You just have to write the Reader() and
  16. **   Writer() code.
  17. *)
  18.  
  19. unit RW_IO;
  20.  
  21. interface
  22.  
  23. type
  24.   String40 = String[40];
  25.   String12 = String[12];
  26.  
  27. function ReaderOpen(Filename : String12) : Integer;
  28. function Reader : Integer;
  29. function ReaderClose : Integer;
  30. function ReaderCount : LongInt;
  31.  
  32. function WriterOpen(Filename : String12) : Integer;
  33. function Writer(TheByte : Byte) : Integer;
  34. function WriterClose : Integer;
  35. function WriterCount : LongInt;
  36. function DummyWrite(TheByte : Byte) : Integer;
  37.  
  38. implementation
  39.  
  40. const
  41.    BUFFER_SIZE = 2048;
  42.  
  43. type
  44.    IOstruct = record
  45.       Filename : String12;
  46.       Handle   : File;
  47.       Left     : Integer;
  48.       Right    : Integer;
  49.       Count    : LongInt;
  50.       Buffer   : array[0..BUFFER_SIZE-1] of Byte;
  51.    end;
  52.  
  53. var
  54.    InpCtrl : IOstruct;
  55.    OutCtrl : IOstruct;
  56.  
  57. function ReaderOpen(Filename : String12) : Integer;
  58. begin
  59.    InpCtrl.Filename := Filename;
  60. {$I-}
  61.    Assign(InpCtrl.Handle,InpCtrl.Filename);
  62.    Reset(InpCtrl.Handle,1);
  63. {$I+}
  64.    InpCtrl.Left := 0;
  65.    InpCtrl.Right := 0;
  66.    InpCtrl.Count := 0;
  67.    ReaderOpen := IOResult;
  68. end;
  69.  
  70. function Reader : Integer;
  71. label 999;
  72. var
  73.   TheByte : Byte;
  74. begin
  75.   if InpCtrl.Left=InpCtrl.Right then
  76.      begin
  77.         (* read next buffer *)
  78.         InpCtrl.Left := 0;
  79.         BlockRead(InpCtrl.Handle,InpCtrl.Buffer,BUFFER_SIZE,InpCtrl.Right);
  80.         if InpCtrl.Right <= 0 then
  81.            begin
  82.              Reader := -1;
  83.              goto 999;
  84.            end;
  85.      end;
  86.   (* return next byte in buffer *)
  87.   TheByte := InpCtrl.Buffer[InpCtrl.Left];
  88.   InpCtrl.Left := InpCtrl.Left + 1;
  89.   InpCtrl.Count := InpCtrl.Count + 1;
  90.   Reader := TheByte;
  91. 999 : end;
  92.  
  93. function ReaderClose : Integer;
  94. begin
  95.   close(InpCtrl.Handle);
  96. end;
  97.  
  98. function ReaderCount : LongInt;
  99. begin
  100.   ReaderCount := InpCtrl.Count;
  101. end;
  102.  
  103. function WriterOpen(Filename : String12) : Integer;
  104. begin
  105.    OutCtrl.Filename := Filename;
  106. {$I-}
  107.    Assign(OutCtrl.Handle,OutCtrl.Filename);
  108.    Rewrite(OutCtrl.Handle,1);
  109. {$I+}
  110.    OutCtrl.Left := 0;
  111.    OutCtrl.Right := 0;
  112.    OutCtrl.Count := 0;
  113.    WriterOpen := IOResult;
  114. end;
  115.  
  116. function Writer(TheByte : Byte) : Integer;
  117. begin
  118.   OutCtrl.Count := OutCtrl.Count + 1;
  119.   if (OutCtrl.Count and $0fff) = 0 then write('.');
  120.   OutCtrl.Buffer[OutCtrl.Right] := TheByte;
  121.   OutCtrl.Right := OutCtrl.Right + 1;
  122.   if OutCtrl.Right = BUFFER_SIZE then
  123.     begin
  124.       (* write buffer to disk *)
  125.        BlockWrite(OutCtrl.Handle,OutCtrl.Buffer,BUFFER_SIZE);
  126.        OutCtrl.Right := 0;
  127.     end;
  128.   Writer := 0;
  129. end;
  130.  
  131. function WriterClose : Integer;
  132. begin
  133.   if OutCtrl.Right > 0 then
  134.     begin
  135.        BlockWrite(OutCtrl.Handle,OutCtrl.Buffer,OutCtrl.Right);
  136.        OutCtrl.Right := 0;
  137.     end;
  138.   close(OutCtrl.Handle);
  139. end;
  140.  
  141. function WriterCount : LongInt;
  142. begin
  143.   WriterCount := OutCtrl.Count;
  144. end;
  145.  
  146. function DummyWrite(TheByte : Byte) : Integer;
  147. begin
  148.   DummyWrite := 0;
  149. end;
  150.  
  151. end.